Chapter 1 - Getting Started with Variables and Values

This notebook uses code snippets and explanations from this course.

Welcome to the course! In this course we will learn how to load, process and save data using a versatile programming language: Python. We are going to practise Python using Notebooks. These Notebooks contain instructions and so called 'code blocks'. The instructions are paragraphs of text that explain the concepts we are going to use. The 'code blocks' contain Python code.

Notebooks are pretty straightforward. Some tips:

  • Cells in a notebook contain code or text. If you run a cell, it will either run the code or render the text.
  • There are five ways to run a cell:
    1. Click the 'play' button next to the 'stop' and 'refresh' button in the toolbar.
    2. Alt + Enter runs the current cell and creates a new cell.
    3. Ctrl + Enter runs the current cell without creating a new cell. (Cmd + Enter on a Mac.)
    4. Shift + Enter runs the current cell and moves to the next one.
    5. Use the menu and select Cell/Run all.
  • The instructions are written in Markdown. Here is a nice Markdown cheatsheet if you want to write some more.
  • Explore the menus for more options! You can even create a presentation using Notebooks.

Hint: when you're writing Python code, press Tab to auto-complete your variable names!

At the end of this chapter, you will be able to:

  • print information to your screen using the built-in function print()
  • assign values to variables (using valid and clear variable names)
  • do calculations in Python

If you have questions about this chapter, please refer to the forum on Canvas.

Now let's get started!

1. Getting started together

1.1 Hello, world!

The best way to learn Python is by jumping right in. Let's first start with something really simple. Every programming language is traditionally introduced with a "Hello world" example. Please run the following cell:


In [ ]:
# this will print some text
print("Hello, world!")

What happened here? Well, Python has a large set of built-in functions, and print() is one of them. When you use this function, print() outputs its argument to the screen. 'Argument' is a fancy word for "object you put in a function". In this case, the argument is the string "Hello, world!". And 'string' just means "a sequence of characters".

Did you also notice the first line starting with a hash (#) character? This is called a comment. We use comments to document our code and explain what's happening. These lines are not executed in Python. We will use them a lot in this course to make our code easy to understand!

Can you edit the block below in such a way that it will print out your own name?


In [ ]:
print("Hello, world!")

1.2 Calculations

Apart from printing some text to your screen, you can also use Python to do calculations.


In [ ]:
# summing
print(3+2)

# subtracting
print(7-1)

In [ ]:
# multiplication
print(3*3)

# division
print(10/3)

In [ ]:
# power
print(5**2)

# combining stuff
print(5*2-3+4/2)

2. Variables and values

Instead of providing the string directly as an argument to the print function, we can also create a variable that refers to the string value "Hello, world!". When you pass this variable to the print() function, you get the same result as before:


In [ ]:
text = "Hello, world!"
print(text)

Such a piece of text ("Hello, world!") is called a string in Python (cf. a string of characters). Strings in Python must always be enclosed with 'quotes' (e.g. single or double quotes). Without those quotes, Python will think it's dealing with the name of some variable that has been defined earlier, because variable names never take quotes. The following distinction is confusing, but extremely important: variable names (without quotes) and string values (with quotes) look similar, but they serve a completely different purpose. Compare:


In [ ]:
name = "Patrick Bateman"
print("name")   # this is a string value
print(name)    # this is a variable name containing a string value

We can also assign numerical values to variables:


In [ ]:
x = 22
print(x)

If you vaguely remember your math classes in school, this should look familiar. It is basically the same notation with the name of the variable on the left, the value on the right, and the '=' sign in the middle. This is what is called assignment. We stored a value and named it using the '=' symbol, so that we can easily use it later on.

We can use the box metaphor to further explain this concept. The variable x above behaves pretty much like a box on which we write an x with a thick, black marker to find it back later. In this box we can put whatever we want, such as a piece of text or a numerical value. In Python, the term variable refers to such a box, whereas the term value refers to what is inside this box.

Note that we can re-use variable names for other values, but that any assignment will overwrite the original value! In other words: when you re-asign a variable, you remove the content of the box and put something new in it. Each variable will always contain the value that you last assigned to it.


In [ ]:
text = "I like apples"
print(text)
text = "I like oranges"
print(text)

When we have stored values inside variables, we can do interesting things with these variables. Run the following code block to see what happens.


In [ ]:
x = 3
print(x)
print(x * x)
print(x + x)
print(x - 6)

2.1 Variable names

Note that the variable names text and x used above are not part of Python. In principle, you could use any name you like. Even if you change the variable text to something silly like pikachu or sniffles, the example would still work:


In [ ]:
sniffles = "Hello, world!"
print(sniffles)

However, variable names are only valid if they:

  • start with a letter or underscore (_)
  • only contain letters, numbers and underscores

Even though you could use any variable name as long as they are valid, there are some naming conventions that are explained in the PEP8 Style Guide for Python Code. For now, it's enough to remember the following for naming your variables:

  • use clear, meaningful, descriptive variable names so that your code will remain understandable
  • use the lowercase_with_underscores style, with lowercase characters and underscores for separating words
  • do not use built-in names, such as print or sum (these will turn green in Jupyter Notebooks)

For example, the following variable name is valid, much more descriptive than x would be and follows the naming conventions:


In [ ]:
seconds_in_seven_years = 220752000
print(seconds_in_seven_years)

2.2 Copying/referencing variables

We can also 'copy' the contents of a variable into another variable, which is what happens in the code below. In fact, what is happening is that the variable second_number now refers to the same data object as first_number. You should of course watch out in such cases: make sure that you keep track of the value of each individual variable in your code (later in the course, we will see that this is especially tricky with data types that are mutable, such as lists).


In [ ]:
first_number = 5
print(first_number)
second_number = first_number
first_number = 3
print(first_number)
print(second_number)

Have a look at this code at Python Tutor to see what's happening!

2.3 User input

Up until now we have defined the values stored in the variables ourselves. However, we can also ask for input from a user. We'll make use of another built-in function: input(). This takes user input and returns it as a string. Try it below:


In [ ]:
text = input("Please enter some text: ")
print(text)

Exercises

Exercise 1:

Use Python as a calculator to calculate the number of seconds in seven years (in one line of code). Assign the output to a variable with a clear variable name. Print the result.


In [ ]:
# your code here

Exercise 2:

Rewrite the code you above before by assigning each of the numerical values to a variable with a clear name. Then use these variables to calculate the number of seconds in seven years.


In [ ]:
days_in_year = 365
# assign each of the values to meaningful variable names

seconds_in_seven_years = days_in_year * # finish this line
print(seconds_in_seven_years)

Exercise 3:

Run the following code block and see what happens. Can you fix the invalid variable names? You should get no error in the end.


In [ ]:
eggs = 3
_eggs = 6
5eggs = 5
eggs$ = 1
eggs123 = 9
ten_eggs = 10
TwelveEggs = 8
twelve.eggs = 12

Exercise 4:

Can you write some code that swaps the values of these two variables? Hint: create an extra variable.


In [ ]:
first_number = 3
second_number = 5

Exercise 5:

Write a program that asks two people for their names using input(). Store the names in variables called name1 and name2. Say hello to both of them.


In [ ]:
# adapt the code
name1 = "Paul"
print("Hello,", name1)